package org.pkl.tests;

import java.util.ArrayList;
import java.util.List;

import com.gadberry.utility.algorithm.Algorithm;
import com.gadberry.utility.expression.ArgumentCastException;
import com.gadberry.utility.expression.Expression;
import com.gadberry.utility.expression.InvalidExpressionException;
import com.gadberry.utility.expression.OperatorSet;


public class Etest001 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			//test001();
			fib002();
		} catch (ArgumentCastException e) {
			e.printStackTrace();
		} 
	}

//	public static void token001(){
//		OperatorSet opset = OperatorSet.getStandardOperatorSet();
//		OperatorSet.addAlgorithmOperatorSet();
//		
//		String expr = "2 ^ (3 + 1)";
//		
//		List<String> tokens = Expression.tokenize(null, expr, opset.getDelimeters());
//		System.out.println("----------------------------------------------");
//		for(String token : tokens)
//			System.out.println(token);
//		System.out.println("----------------------------------------------");
//	}
	
	public static void test001() throws ArgumentCastException{
		OperatorSet.getStandardOperatorSet();

		double n;
		boolean b;

		try {
			n = Expression.evaluate("pow(2,3)").toDouble();
			System.out.println(n);

			n = Expression.evaluate("2 ^ (3 + 1)").toDouble();
			System.out.println(n);

			n = Expression.evaluate("max(2, min(5,3)) + 2 * 2").toDouble();
			System.out.println(n);

			n = Expression.evaluate("max 2,3 + 2 * 2").toDouble();
			System.out.println(n);

			b = Expression.evaluate("3 < 4 AND max(20,22) >= 26 OR 2 > 9").toBoolean();
			System.out.println(b);
		} catch (InvalidExpressionException e) {
			e.printStackTrace();
		}

	}

	public static void test002(){
		OperatorSet.getStandardOperatorSet();
		OperatorSet.addAlgorithmOperatorSet();

		ArrayList<String> lines = new ArrayList<String>();
		lines.add("'nbr' := 3.5 + 1.2");
		lines.add("IF($nbr < 10.0)");
		lines.add("'nbr' := ($'nbr' + 100)");
		lines.add("ELSE");
		lines.add("'nbr' := ($'nbr' - 100)");
		lines.add("ENDIF");
		
		Algorithm algor = new Algorithm(lines);
		algor.printListing(true);
		algor.setTrace(true);
		algor.execute();
		algor.printVars();
	}

	public static void test003(){
		OperatorSet.getStandardOperatorSet();
		OperatorSet.addAlgorithmOperatorSet();

		ArrayList<String> lines = new ArrayList<String>();
		lines.add("'nbr' := 3.5 + 1.2");
		lines.add("REPEAT");
		lines.add("'nbr' := ($'nbr' + 100)");
		lines.add("'nbr' := ($'nbr' * 1.05)");
		lines.add("println( '    > ', $'nbr')");
		lines.add("UNTIL ($'nbr' > 200)");

		Algorithm algor = new Algorithm(lines);
		algor.printListing(true);
		algor.setTrace(false);
		algor.execute();
	}

	public static void test004(){
		OperatorSet.getStandardOperatorSet();
		OperatorSet.addAlgorithmOperatorSet();

		ArrayList<String> lines = new ArrayList<String>();
		lines.add("'nbr' := 3.5 + 1.2");
		lines.add("WHILE ($'nbr' < 1000)");
		lines.add("'nbr' := ($'nbr' + 100)");
		lines.add("'nbr' := ($'nbr' * 1.2)");
		lines.add("println( '    > ', $'nbr')");
		lines.add("WEND");

		Algorithm algor = new Algorithm(lines);
		algor.printListing(true);
		algor.setTrace(false);
		algor.execute();
	}

	public static void fib001(){
		OperatorSet.getStandardOperatorSet();
		OperatorSet.addAlgorithmOperatorSet();

		ArrayList<String> lines = new ArrayList<String>();
		lines.add("'a' := ( 'b' := 1) ");
		lines.add("'count' := 2");
		lines.add("print($'a', ' ', $'b', ' ') ");
		lines.add("REPEAT");
		lines.add("  'c' := int($'a' + $'b')");
		lines.add("  print($'c', ' ') ");
		lines.add("  'a' := $'b' ");
		lines.add("  'b' := $'c' ");
		lines.add("  'count' := $'count' + 1 ");
		lines.add("UNTIL ($'count' == 8)");

		Algorithm algor = new Algorithm(lines);
		algor.printListing(true);
		algor.setTrace(false);
		algor.execute();
	}
	
	public static void fib002(){
		OperatorSet.getStandardOperatorSet();
		OperatorSet.addAlgorithmOperatorSet();

		ArrayList<String> lines = new ArrayList<String>();
		lines.add("'a' := ( 'b' := 1) ");
		lines.add("'count' := 2");
		lines.add("print($'a', ' ', $'b', ' ') ");
		lines.add("WHILE($'count' < 8) ");
		lines.add("  'c' := int($'a' + $'b')");
		lines.add("  print($'c', ' ') ");
		lines.add("  'a' := $'b' ");
		lines.add("  'b' := $'c' ");
		lines.add("  'count' := $'count' + 1 ");
		lines.add("WEND");
		lines.add("println ");

		Algorithm algor = new Algorithm(lines);
		algor.printListing(true);
		algor.setTrace(false);
		algor.execute();
	}
}
